home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection 1998 Fall: Game Toolkit / Disc.iso / SDKs / Third Party SDKs / ATI RAVE SDK / Samples / QD3D Tests / BackGround (Original) / BackGroundSupport.c < prev    next >
Encoding:
Text File  |  1998-07-07  |  19.1 KB  |  717 lines  |  [TEXT/CWIE]

  1. // Quickdraw 3D sample code
  2. //
  3. // This file illustrates how to set up a pixmap based draw context.
  4. // A metafile is read into and imaged in the pixmap, this pixmap is combined
  5. // with a pixmap containing a background, so that the 3d data is drawn over 
  6. // the background
  7. //
  8. // Nick Thompson, AppleLink: DEVSUPPORT (devsupport@applelink.apple.com)
  9. //
  10. // ©1994-5 Apple Computer Inc., All Rights Reserved
  11.  
  12.  
  13.  
  14. #include <Files.h>
  15. #include <QuickDraw.h>
  16. #include <QDOffScreen.h>
  17. #include <StandardFile.h>
  18.  
  19. #include "BackGroundSupport.h"
  20.  
  21. #include "QD3D.h"
  22. #include "QD3DDrawContext.h"
  23. #include "QD3DRenderer.h"
  24. #include "QD3DShader.h"
  25. #include "QD3DCamera.h"
  26. #include "QD3DLight.h"
  27. #include "QD3DGeometry.h"
  28. #include "QD3DGroup.h"
  29. #include "QD3DMath.h"
  30. #include "QD3DTransform.h"
  31. #include "QD3DStorage.h"
  32. #include "QD3DIO.h"
  33.  
  34. #undef kQ3RealZero            // temp chrisb
  35. #define kQ3RealZero 0.0
  36.  
  37. //-----------------------------------------------------------------------------------------------
  38. // local utility functions
  39. static    TQ3FileObject         MyGetNewFile( FSSpec *myFSSpec, TQ3Boolean *isText ) ;
  40.  
  41. static void GetGroupBBox(
  42.     DocumentPtr            theDocument,
  43.     TQ3BoundingBox         *viewBBox) ;
  44.                                                 
  45. static    TQ3Status MyAddShaderToGroup( TQ3GroupObject group ) ;
  46.  
  47. static TQ3Status GetDocumentGroupBoundingBox( 
  48.     DocumentPtr theDocument , 
  49.     TQ3BoundingBox *viewBBox) ;
  50.  
  51. //-----------------------------------------------------------------------------------------------
  52. // Submit the scene for rendering/fileIO and picking
  53. TQ3Status SubmitScene( DocumentPtr theDocument ) 
  54. {        
  55.     TQ3Vector3D                globalScale;
  56.     TQ3Vector3D                globalTranslate;
  57.     
  58.     globalScale.x = globalScale.y = globalScale.z = theDocument->fGroupScale;
  59.     globalTranslate = *(TQ3Vector3D *)&theDocument->fGroupCenter;
  60.     Q3Vector3D_Scale(&globalTranslate, -1, &globalTranslate);
  61.     Q3Style_Submit(theDocument->fInterpolation, theDocument->fView);
  62.     Q3Style_Submit(theDocument->fBackFacing , theDocument->fView);
  63.     Q3Style_Submit(theDocument->fFillStyle, theDocument->fView);
  64.         
  65.     Q3MatrixTransform_Submit( &theDocument->fRotation, theDocument->fView);
  66.         
  67.     Q3ScaleTransform_Submit(&globalScale, theDocument->fView);
  68.     Q3TranslateTransform_Submit(&globalTranslate, theDocument->fView);
  69.     Q3DisplayGroup_Submit( theDocument->fModel, theDocument->fView);
  70.     
  71.     return kQ3Success ;
  72. }
  73.  
  74. //-----------------------------------------------------------------------------------------------
  75.  
  76. static TQ3Status GetDocumentGroupBoundingBox( 
  77.     DocumentPtr theDocument , 
  78.     TQ3BoundingBox *viewBBox)
  79. {
  80.     TQ3Status        status;
  81.     TQ3ViewStatus    viewStatus ;
  82.     
  83.     status = Q3View_StartBoundingBox( theDocument->fView, kQ3ComputeBoundsApproximate );
  84.     do {
  85.         status = SubmitScene( theDocument ) ;
  86.     } while((viewStatus = Q3View_EndBoundingBox( theDocument->fView, viewBBox )) == kQ3ViewStatusRetraverse );
  87.     return status ;
  88. }
  89. //-----------------------------------------------------------------------------------------------
  90.  
  91. TQ3ViewObject MyNewView(GWorldPtr myOffscreenGWorld)
  92. {
  93.     TQ3Status                myStatus;
  94.     TQ3ViewObject            myView;
  95.     TQ3DrawContextObject        myDrawContext;
  96.     TQ3RendererObject        myRenderer;
  97.     TQ3CameraObject            myCamera;
  98.     TQ3GroupObject            myLights;
  99.     
  100.     if((myView = Q3View_New()) == NULL)
  101.         goto bail ;    
  102.         
  103.     //    Create and set draw context.
  104.     if ((myDrawContext = MyNewDrawContext(myOffscreenGWorld)) == NULL )
  105.         goto bail;
  106.         
  107.     if ((myStatus = Q3View_SetDrawContext(myView, myDrawContext)) == kQ3Failure )
  108.         goto bail;
  109.  
  110.     Q3Object_Dispose( myDrawContext ) ;
  111.     
  112.     //    Create and set renderer.
  113.     // this would use the interactive software renderer
  114.  
  115.     if ((myRenderer = Q3Renderer_NewFromType(kQ3RendererTypeInteractive)) != NULL ) {
  116.         if ((myStatus = Q3View_SetRenderer(myView, myRenderer)) == kQ3Failure ) {
  117.             goto bail;
  118.         }
  119.     }
  120.     else {
  121.         goto bail;
  122.     }
  123.  
  124.     Q3Object_Dispose( myRenderer ) ;
  125.     
  126.     //    Create and set camera.
  127.     if ( (myCamera = MyNewCamera(myOffscreenGWorld)) == NULL )
  128.         goto bail;
  129.         
  130.     if ((myStatus = Q3View_SetCamera(myView, myCamera)) == kQ3Failure )
  131.         goto bail;
  132.  
  133.     Q3Object_Dispose( myCamera ) ;
  134.     
  135.     //    Create and set lights.
  136.     if ((myLights = MyNewLights()) == NULL )
  137.         goto bail;
  138.         
  139.     if ((myStatus = Q3View_SetLightGroup(myView, myLights)) == kQ3Failure )
  140.         goto bail;
  141.         
  142.     Q3Object_Dispose(myLights);
  143.  
  144.     //    Done!!!
  145.     return ( myView );
  146.     
  147. bail:
  148.     //    If any of the above failed, then don't return a view.
  149.     
  150.     return ( NULL );
  151. }
  152.  
  153. //----------------------------------------------------------------------------------
  154.  
  155. TQ3DrawContextObject MyNewDrawContext(GWorldPtr theGWorld)
  156. {
  157.     TQ3PixmapDrawContextData    myDrawContextData;
  158.     TQ3ColorARGB                clearColor = {1.0, 0.0, 0.0, 0.0} ;
  159.     PixMapHandle                 hPixMap ;
  160.     Rect                        srcRect ;
  161.  
  162.     float                        factor = 0xffff ;
  163.  
  164.     clearColor.r = kClearColor.red / factor ;
  165.     clearColor.g = kClearColor.green / factor ;
  166.     clearColor.b = kClearColor.blue / factor ;
  167.     
  168.     //    Fill in draw context data.
  169.     myDrawContextData.drawContextData.clearImageMethod = kQ3ClearMethodWithColor;
  170.     myDrawContextData.drawContextData.clearImageColor  = clearColor;
  171.  
  172.     myDrawContextData.drawContextData.paneState = kQ3False;
  173.     myDrawContextData.drawContextData.maskState = kQ3False;
  174.     
  175.     myDrawContextData.drawContextData.doubleBufferState = kQ3False;
  176.  
  177.     hPixMap = GetGWorldPixMap(theGWorld);
  178.     LockPixels(hPixMap);
  179.  
  180.     srcRect = theGWorld->portRect;
  181.  
  182.     myDrawContextData.pixmap.width = srcRect.right  - srcRect.left;
  183.     myDrawContextData.pixmap.height= srcRect.bottom - srcRect.top;
  184.     
  185.     myDrawContextData.pixmap.rowBytes = (**hPixMap).rowBytes & 0x7FFF;
  186.     myDrawContextData.pixmap.pixelType = kQ3PixelTypeRGB32;
  187.     myDrawContextData.pixmap.pixelSize = 32;
  188.  
  189.     myDrawContextData.pixmap.bitOrder  = kQ3EndianBig;
  190.     myDrawContextData.pixmap.byteOrder = kQ3EndianBig;
  191.     
  192.     myDrawContextData.pixmap.image        = GetPixBaseAddr( hPixMap );
  193.     
  194.     return Q3PixmapDrawContext_New(&myDrawContextData);
  195. }
  196. //----------------------------------------------------------------------------------
  197.  
  198. TQ3CameraObject MyNewCamera(CGrafPtr thePort)
  199. {
  200.     TQ3CameraObject                    myCamera;
  201.     TQ3CameraData                    myCameraData;
  202.     TQ3ViewAngleAspectCameraData        myViewAngleCameraData;
  203.     TQ3Point3D                        cameraFrom     = { 0.0, 0.0, 30.0 };
  204.     TQ3Point3D                        cameraTo     = { 0.0, 0.0, 0.0 };
  205.     TQ3Vector3D                        cameraUp     = { 0.0, 1.0, 0.0 };
  206.     
  207.     float                             fieldOfView = .52359333333;
  208.     float                             hither         = 0.001;
  209.     float                             yon         = 1000;
  210.     
  211.     //    Fill in camera data.
  212.     myCameraData.placement.cameraLocation = cameraFrom;
  213.     myCameraData.placement.pointOfInterest = cameraTo;
  214.     myCameraData.placement.upVector = cameraUp;
  215.     
  216.     myCameraData.range.hither = hither;
  217.     myCameraData.range.yon = yon;
  218.     
  219.     myCameraData.viewPort.origin.x = -1.0;
  220.     myCameraData.viewPort.origin.y = 1.0;
  221.     myCameraData.viewPort.width = 2.0;
  222.     myCameraData.viewPort.height = 2.0;
  223.     
  224.     myViewAngleCameraData.cameraData = myCameraData;
  225.     myViewAngleCameraData.fov = fieldOfView ;
  226.     
  227.     // set up the aspect ratio based on the window
  228.     myViewAngleCameraData.aspectRatioXToY =  
  229.             (float) (thePort->portRect.right - thePort->portRect.left) / 
  230.             (float) (thePort->portRect.bottom - thePort->portRect.top);
  231.  
  232.     myCamera = Q3ViewAngleAspectCamera_New(&myViewAngleCameraData);    
  233.     
  234.     //    Return the camera.
  235.     return ( myCamera );
  236. }
  237.  
  238.  
  239. //----------------------------------------------------------------------------------
  240.  
  241. TQ3GroupObject MyNewLights()
  242. {
  243.     TQ3GroupPosition            myGroupPosition;
  244.     TQ3GroupObject            myLightList;
  245.     TQ3LightData                myLightData;
  246.     TQ3PointLightData        myPointLightData;
  247.     TQ3DirectionalLightData    myDirectionalLightData;
  248.     TQ3LightObject            myAmbientLight, myPointLight, myFillLight;
  249.     TQ3Point3D                pointLocation = { -10.0, 0.0, 10.0 };
  250.     TQ3Vector3D                fillDirection = { 10.0, 0.0, 10.0 };
  251.     TQ3ColorRGB                WhiteLight = { 1.0, 1.0, 1.0 };
  252.     
  253.     //    Set up light data for ambient light.  This light data will be used for point and fill
  254.     //    light also.
  255.  
  256.     myLightData.isOn = kQ3True;
  257.     myLightData.color = WhiteLight;
  258.     
  259.     //    Create ambient light.
  260.     myLightData.brightness = .2;
  261.     myAmbientLight = Q3AmbientLight_New(&myLightData);
  262.     if ( myAmbientLight == NULL )
  263.         goto bail;
  264.     
  265.     //    Create point light.
  266.     myLightData.brightness = 1.0;
  267.     myPointLightData.lightData = myLightData;
  268.     myPointLightData.castsShadows = kQ3False;
  269.     myPointLightData.attenuation = kQ3AttenuationTypeNone;
  270.     myPointLightData.location = pointLocation;
  271.     myPointLight = Q3PointLight_New(&myPointLightData);
  272.     if ( myPointLight == NULL )
  273.         goto bail;
  274.  
  275.     //    Create fill light.
  276.     myLightData.brightness = .2;
  277.     myDirectionalLightData.lightData = myLightData;
  278.     myDirectionalLightData.castsShadows = kQ3False;
  279.     myDirectionalLightData.direction = fillDirection;
  280.     myFillLight = Q3DirectionalLight_New(&myDirectionalLightData);
  281.     if ( myFillLight == NULL )
  282.         goto bail;
  283.  
  284.     //    Create light group and add each of the lights into the group.
  285.     myLightList = Q3LightGroup_New();
  286.     if ( myLightList == NULL )
  287.         goto bail;
  288.     myGroupPosition = Q3Group_AddObject(myLightList, myAmbientLight);
  289.     if ( myGroupPosition == 0 )
  290.         goto bail;
  291.     myGroupPosition = Q3Group_AddObject(myLightList, myPointLight);
  292.     if ( myGroupPosition == 0 )
  293.         goto bail;
  294.     myGroupPosition = Q3Group_AddObject(myLightList, myFillLight);
  295.     if ( myGroupPosition == 0 )
  296.         goto bail;
  297.  
  298.     Q3Object_Dispose( myAmbientLight ) ;
  299.     Q3Object_Dispose( myPointLight ) ;
  300.     Q3Object_Dispose( myFillLight ) ;
  301.  
  302.     //    Done!
  303.     return ( myLightList );
  304.     
  305. bail:
  306.     //    If any of the above failed, then return nothing!
  307.     return ( NULL );
  308. }
  309.  
  310. //----------------------------------------------------------------------------------
  311.  
  312. TQ3GroupObject MyNewModelFromFile(FSSpec *theFileSpec)
  313. {
  314.     TQ3GroupObject        myGroup = NULL ;
  315.     TQ3Boolean            isText = kQ3False ;
  316.     TQ3FileMode            myFileMode ;    // we are reading the file
  317.     TQ3FileObject        theFile = kQ3FileModeNormal;
  318.     
  319.     //    Create a ordered group for the complete model.
  320.     if ((myGroup = Q3OrderedDisplayGroup_New()) == NULL )
  321.         return NULL;
  322.  
  323.     theFile = MyGetNewFile( theFileSpec, &isText ) ;
  324.     
  325.     if( isText == kQ3True )
  326.         myFileMode |= kQ3FileModeText;    // is it a text metafile??    
  327.  
  328.     // Open the file object
  329.     if( Q3File_OpenRead( theFile, &myFileMode ) != kQ3Success)
  330.         return  NULL ;
  331.  
  332.     MyReadModelFromFile( theFile, myGroup ) ;
  333.     
  334.     Q3File_Close(theFile);            // close and dispose of the file object
  335.     Q3Object_Dispose(theFile);
  336.     
  337.     MyAddShaderToGroup( myGroup ) ;
  338.     
  339.     return myGroup ;
  340. }
  341.  
  342.  
  343. //----------------------------------------------------------------------------------
  344. // attach a shader to the group
  345.  
  346. TQ3Status MyAddShaderToGroup( TQ3GroupObject group )
  347. {
  348.     TQ3ShaderObject    illuminationShader = Q3PhongIllumination_New();
  349.  
  350.     Q3Group_AddObject(group, illuminationShader);
  351.     Q3Object_Dispose(illuminationShader);
  352.     return(kQ3Success);
  353. }
  354.  
  355. //----------------------------------------------------------------------------------
  356. // read model from file object into the supplied group
  357.  
  358. TQ3Status MyReadModelFromFile( TQ3FileObject theFile,TQ3GroupObject myGroup)
  359. {    
  360.     if(theFile != NULL) {
  361.     
  362.         TQ3Object            myTempObj ;
  363.         TQ3Boolean            isEOF ;
  364.                 
  365.     
  366.         // read objects from the file
  367.         do {
  368.         
  369.             myTempObj = Q3File_ReadObject( theFile );
  370.             
  371.             if( myTempObj != NULL ) {
  372.                 // we only want the object in our main group if we can draw it
  373.                 if (Q3Object_IsDrawable( myTempObj) ) 
  374.                     Q3Group_AddObject( myGroup, myTempObj ) ;
  375.                 
  376.                 // we either added the object to the main group, or we don't care
  377.                 // so we can safely dispose of the object
  378.                 Q3Object_Dispose( myTempObj ) ;
  379.             }
  380.             
  381.             // check to see if we reached the end of file yet
  382.             isEOF = Q3File_IsEndOfFile( theFile );
  383.             
  384.         } while (isEOF == kQ3False);    
  385.     }
  386.     
  387.     if( myGroup != NULL )
  388.         return kQ3Success ;
  389.     else
  390.         return kQ3Failure ;
  391. }
  392.  
  393. //-----------------------------------------------------------------------------------------------
  394. // cleaned up from IM QuickDraw 3D pp 15-5
  395. static TQ3FileObject MyGetNewFile( FSSpec *myFSSpec, TQ3Boolean *isText )
  396. {
  397.     TQ3FileObject        myFileObj;
  398.     TQ3StorageObject        myStorageObj;
  399.     OSType                myFileType;
  400.     
  401.     FInfo                fndrInfo ;
  402.  
  403.     // we assume the FSSpec passed in was valid, get the file information
  404.     // we need to know the file type, this routine may get called by an appleEvent
  405.     // handler, so we can't assume a type, we need to get it from the fsspec.
  406.     
  407.     FSpGetFInfo( myFSSpec, &fndrInfo ) ;
  408.     
  409.     // pull out the file type
  410.     
  411.     myFileType = fndrInfo.fdType ;
  412.     
  413.     // Create new storage object and new file object 
  414.     if(((myStorageObj = Q3FSSpecStorage_New( myFSSpec )) == NULL) 
  415.         || ((myFileObj = Q3File_New()) == NULL)) 
  416.     {
  417.         if (myStorageObj != NULL) 
  418.             Q3Object_Dispose(myStorageObj);
  419.         return(NULL);
  420.     }
  421.  
  422.     // Set the storage for the file object
  423.     Q3File_SetStorage(myFileObj, myStorageObj);
  424.     Q3Object_Dispose(myStorageObj);
  425.  
  426.     if (myFileType == '3DMF')
  427.         *isText = kQ3False ;
  428.     else if (myFileType == 'TEXT')
  429.         *isText = kQ3True ;
  430.  
  431.     return (myFileObj);
  432. }
  433.  
  434.  
  435. //-------------------------------------------------------------------------------------------
  436. //
  437. Boolean MetafileFileSpecify( FSSpec *theFile )
  438. {
  439.     StandardFileReply    theSFReply ;
  440.     SFTypeList            myTypes = { '3DMF' } ;
  441.     const short            numTypes = 1 ;
  442.         
  443.     // Get the file name to open
  444.     StandardGetFile( nil, numTypes, myTypes, &theSFReply ) ;
  445.     
  446.     if( theSFReply.sfGood )
  447.         *theFile = theSFReply.sfFile ;
  448.     
  449.     // did the user cancel?
  450.     return theSFReply.sfGood ;
  451.     
  452. }
  453. //-------------------------------------------------------------------------------------------
  454. //
  455. Boolean PictureFileSpecify( FSSpec *theFile )
  456. {
  457.     StandardFileReply    theSFReply ;
  458.     SFTypeList            myTypes = { 'PICT' } ;
  459.     const short            numTypes = 1 ;
  460.         
  461.     // Get the file name to open
  462.     StandardGetFile( nil, numTypes, myTypes, &theSFReply ) ;
  463.     
  464.     if( theSFReply.sfGood )
  465.         *theFile = theSFReply.sfFile ;
  466.     
  467.     // did the user cancel?
  468.     return theSFReply.sfGood ;
  469.     
  470. }
  471.  
  472. //----------------------------------------------------------------------------------
  473. PicHandle OpenPICTFile( FSSpec *theFile )
  474. {
  475.     OSErr        err;
  476.     long        curEOF;
  477.     PicHandle    my_pic;
  478.     long         count;
  479.     Ptr         buffer;
  480.     short        refNum;
  481.     
  482.     if (FSpOpenDF(theFile, fsRdWrPerm, &refNum))
  483.         return(0);
  484.  
  485.     /* get size of file */
  486.     err = GetEOF(refNum, &curEOF);
  487.     if (err != 0) {
  488.         return(0);
  489.     }
  490.     
  491.     /* move the file mark to 512 */
  492.     err = SetFPos(refNum, fsFromStart, 512L);
  493.     if (err != 0) {
  494.         return(0);
  495.     }
  496.  
  497.     /* size of data to read */
  498.     count = curEOF - 512;
  499.     
  500.     /* create the PicHandle */
  501.     my_pic = (PicHandle)NewHandle(count);
  502.     HLock((Handle)my_pic);
  503.     
  504.     /* read the PICT info */
  505.     buffer = (Ptr)(*my_pic);
  506.     err = FSRead(refNum, &count, buffer);
  507.     if (err != 0) {
  508.         return(0);
  509.     }
  510.     HUnlock((Handle)my_pic);
  511.  
  512.     FSClose(refNum);
  513.     
  514.     return (my_pic);
  515. }
  516. //----------------------------------------------------------------------------------
  517.  
  518.  
  519. void GetGroupBBox(
  520.     DocumentPtr            theDocument,
  521.     TQ3BoundingBox         *viewBBox)
  522. {
  523.     TQ3Point3D                     from     = { 0.0, 0.0, 1.0 };
  524.     TQ3Point3D                     to         = { 0.0, 0.0, 0.0 };
  525.     TQ3Vector3D                     up         = { 0.0, 1.0, 0.0 };
  526.     
  527.     float                         fieldOfView = .52359333333;
  528.     float                         hither         =  0.5;
  529.     float                         yon         =  1.5;
  530.     TQ3GroupObject                mainGroup = theDocument->fModel ;
  531.  
  532.     TQ3Status                    status;
  533.     
  534. #ifdef BETA_1_BUILD
  535.     Q3View_StartBounds( theDocument->fView );
  536.  
  537.     status = Q3DisplayGroup_BoundingBox(mainGroup, 
  538.                                         viewBBox, 
  539.                                         kQ3ComputeBoundsApproximate,
  540.                                          viewObject);
  541.  
  542.     Q3View_EndBounds( theDocument->fView );
  543. #else
  544.     status = GetDocumentGroupBoundingBox( theDocument , viewBBox) ;
  545. #endif
  546.                                         
  547.     //
  548.     //  If we have a point model, then the "viewBBox" would end up
  549.     //  being a "singularity" at the location of the point.  As
  550.     //  this bounding "box" is used in setting up the camera spec,
  551.     //  we get bogus input into Escher.
  552.     
  553.     {
  554.          float        xSize, ySize, zSize;
  555.         
  556.         xSize = viewBBox->max.x - viewBBox->min.x;
  557.         ySize = viewBBox->max.y - viewBBox->min.y;
  558.         zSize = viewBBox->max.z - viewBBox->min.z;
  559.  
  560.         if (xSize <= kQ3RealZero &&
  561.             ySize <= kQ3RealZero &&
  562.             zSize <= kQ3RealZero) {
  563.             
  564.             viewBBox->max.x += 0.0001;
  565.             viewBBox->max.y += 0.0001;
  566.             viewBBox->max.z += 0.0001;
  567.             
  568.             viewBBox->min.x -= 0.0001;
  569.             viewBBox->min.y -= 0.0001;
  570.             viewBBox->min.z -= 0.0001;
  571.         }
  572.     }
  573. }
  574.  
  575.  
  576.  
  577.  
  578. //------------------------------------------------------------------------
  579.  
  580.  
  581. TQ3Point3D AdjustCamera(
  582.     DocumentPtr            theDocument,
  583.     short                winWidth,
  584.     short                winHeight)
  585. {
  586.     float                         fieldOfView;
  587.     float                         hither;
  588.     float                         yon;
  589.     TQ3CameraPlacement            placement;
  590.     TQ3CameraRange                range;
  591.     TQ3BoundingBox                 viewBBox;
  592.     long                         fromAxis;    
  593.     float                         maxDimension;
  594.      float                        xSize, ySize, zSize;
  595.     float                        weights[2] = { 0.5, 0.5 };
  596.     TQ3Point3D                    points[2];
  597.     TQ3Vector3D                     viewVector;
  598.     TQ3Vector3D                    normViewVector;
  599.     TQ3Vector3D                    eyeToFrontClip;
  600.     TQ3Vector3D                    eyeToBackClip;
  601.     float                        viewDistance;
  602.     TQ3Vector3D                    diagonalVector;
  603.     float                        ratio;
  604.     TQ3CameraObject                camera;
  605.     
  606.     TQ3ViewObject                theView = theDocument->fView ;
  607.     TQ3GroupObject                mainGroup = theDocument->fModel ;
  608.     
  609.     TQ3Point3D                    *documentGroupCenter = &theDocument->fGroupCenter ;
  610.     float                        *documentGroupScale  = &theDocument->fGroupScale ;
  611.  
  612.     Q3View_GetCamera( theView, &camera);
  613.     GetGroupBBox( theDocument, &viewBBox);
  614.  
  615.     /*
  616.      *  If we have a point model, then the "viewBBox" would end up
  617.      *  being a "singularity" at the location of the point.  As
  618.      *  this bounding "box" is used in setting up the camera spec,
  619.      *  we get bogus input into Escher.
  620.      */
  621.     xSize = viewBBox.max.x - viewBBox.min.x;
  622.     ySize = viewBBox.max.y - viewBBox.min.y;
  623.     zSize = viewBBox.max.z - viewBBox.min.z;
  624.  
  625.     if (xSize <= kQ3RealZero &&
  626.         ySize <= kQ3RealZero &&
  627.         zSize <= kQ3RealZero)  {
  628.         viewBBox.max.x += 0.0001;
  629.         viewBBox.max.y += 0.0001;
  630.         viewBBox.max.z += 0.0001;
  631.         
  632.         viewBBox.min.x -= 0.0001;
  633.         viewBBox.min.y -= 0.0001;
  634.         viewBBox.min.z -= 0.0001;
  635.     }
  636.  
  637.     points[0] = viewBBox.min;
  638.     points[1] = viewBBox.max;
  639.  
  640.     Q3Point3D_AffineComb(points, weights, 2, documentGroupCenter);
  641.  
  642.     /*
  643.      *  The "from" point is on a vector perpendicular to the plane
  644.      *  in which the bounding box has greatest dimension.  As "up" is
  645.      *  always in the positive y direction, look at x and z directions.
  646.      */
  647.     xSize = viewBBox.max.x - viewBBox.min.x;
  648.     zSize = viewBBox.max.z - viewBBox.min.z;
  649.     
  650.     if (xSize > zSize) {
  651.         fromAxis = kQ3AxisZ;
  652.     } else {
  653.         fromAxis = kQ3AxisX;
  654.     }
  655.  
  656.     /*
  657.      *  Compute the length of the diagonal of the bounding box.
  658.      *
  659.      *  The hither and yon planes are adjusted so that the
  660.       *  diagonal of the bounding box is 7/8 the size of the
  661.       *  minimum dimension of the view frustum. The diagonal is used instead
  662.       *  of the maximum size (in x, y, or z) so that when you rotate
  663.       *  the object, the corners don't get clipped out.
  664.       */
  665.     Q3Point3D_Subtract(
  666.         &viewBBox.max,
  667.         &viewBBox.min,
  668.         &diagonalVector);
  669.  
  670.     maxDimension    =    Q3Vector3D_Length(&diagonalVector);
  671.     maxDimension    *=    8.0 / 7.0;
  672.     
  673.     ratio = 1.0 / maxDimension;
  674.             
  675.     *documentGroupScale = ratio;
  676.     
  677.     Q3Camera_GetPlacement(camera, &placement);
  678.  
  679.     Q3Point3D_Subtract(
  680.         &placement.cameraLocation,
  681.         &placement.pointOfInterest,
  682.         &viewVector);
  683.         
  684.     viewDistance = Q3Vector3D_Length(&viewVector);
  685.     
  686.     Q3Vector3D_Normalize(&viewVector, &normViewVector);
  687.     
  688.     Q3Vector3D_Scale(&normViewVector, 
  689.                      viewDistance - ratio * maxDimension/2.0,
  690.                      &eyeToFrontClip);
  691.                     
  692.     Q3Vector3D_Scale(&normViewVector, 
  693.                     viewDistance + ratio * maxDimension/2.0,
  694.                     &eyeToBackClip);
  695.  
  696.     hither     = Q3Vector3D_Length(&eyeToFrontClip);
  697.     yon     = Q3Vector3D_Length(&eyeToBackClip);
  698.     
  699.     fieldOfView = 2 * atan((ratio * maxDimension/2.0)/hither);
  700.  
  701.     range.hither                 = hither;
  702.     range.yon                     = yon;
  703.  
  704.     Q3Camera_SetRange(camera, &range);
  705.  
  706.     Q3ViewAngleAspectCamera_SetFOV(
  707.         camera, fieldOfView);
  708.  
  709.     Q3ViewAngleAspectCamera_SetAspectRatio(
  710.         camera, (float) winWidth / (float) winHeight);
  711.  
  712.     Q3Object_Dispose(camera);
  713.     
  714.     return( *documentGroupCenter );
  715. }
  716.  
  717.